<?php 
/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Input
*/
class DomFgSelect extends DomFgInput {
	
	/**
	* @var Array
	*/
	protected $selected;
	
	protected $values;
	
	/**
	* Object property that will be used to get select option values for showing to user. Id will be u
	* @var String
	*/
	protected $property;
	
	/**
	* Object property that will be used to get select option values.
	* @var String
	*/
	protected $keyProperty;
	
	public function __construct( $name = NULL, array $values = array(), $selected = NULL ) {
		parent::__construct( $name );
		$this->values( $values );
		if( $selected !== NULL )
			$this->selected( array($selected) );
	}
	
	public function width( $width ) {
		$this->style .= 'width: ' . $width . (is_int($width) ? 'px' : NULL).';';
		return $this;
	}
	
	public static function parseArrayInput( array $inputValues, array $selected, $property, $keyProperty ) {
		$values = array();
		
		if( $property == NULL ) {
			$property = 'name';
		}
		
		if( $keyProperty == NULL ) {
			$keyProperty = 'id';
		}

		foreach( $inputValues as $key => $value) {

			// format: array(key=>KEY, value=>VALUE, level=>LEVEL, style=STRING, disabled=bool)
			if( is_array($value) && isset($value[$keyProperty]) && isset($value[$property]) )
				$fValue = array('value' => $value[$property], 'key'=>$value[$keyProperty], 'level'=>(int)$value['level'], 'style' => (string)$value['style'], 'disabled' => (bool)$value['disabled'] );
					
			// format: KEY => array(value=>VALUE, level=>LEVEL, style=STRING, disabled=bool)
			else if( is_array($value) && isset($value[$property]) )
				$fValue = array('value' => $value[$property], 'key'=>$key, 'level'=>(int)$value['level'], 'style' => (string)$value['style'], 'disabled' => (bool)$value['disabled'] );
			
			// format: array(0=>KEY, 1=>VALUE, 2=>LEVEL, style=STRING, disabled=bool)
			else if( is_array($value) && isset($value[0]) && isset($value[1]) )
				$fValue = array('value' => $value[1], 'key'=>$value[0], 'level'=>(int)(isset($value[2]) ? $value[2] : $value['level']), 'style' => (string)$value['style'], 'disabled' => (bool)$value['disabled'] );

			// format: KEY=>VALUE
			else if( is_string($value) or is_numeric($value) or is_null($value) )
				$fValue = array('value' => $value, 'key'=>$key);
			
			// format: OBJECT
			else if( is_object($value) )
				$fValue = array('value'=> $value->$property, 'key'=>$value->$keyProperty);
			
			else
				throw new RuntimeException('Unknown select value format! (property: '.$property.', keyProperty: '.$keyProperty.')');

			$fValue['selected'] = $selected[$fValue['key']]; 
			//in_array($fValue['key'], $selected);
			
			$fValue['value'] = $fValue['value'];
			$fValue['key'] = htmlspecialchars($fValue['key']);
			
			$values[] = $fValue;
		}
		return $values;
	}
	
	public static function setSelected( array &$values, array $selected, $keyProperty ) {
		if( $keyProperty == NULL )
			$keyProperty  = 'id';
		
		if( count($values) > 0 ) {
			foreach( $values as &$value )
				$value['selected'] = (bool)$selected[$value[$keyProperty]];
				//in_array($value['key'], $selected);
		}
		return $values;
	}
	
	public function values( array $values ) {
		$this->values = $values;
		return $this;
	}
	
	public function selected( $selected ) {
		$this->selected = $selected;
		return $this;
	}
	
	public function property( $property ) {
		$this->property = $property;
		return $this;
	}
	
	public function keyProperty( $keyProperty ) {
		$this->keyProperty = $keyProperty;
		return $this;
	}
	
	public static function parsePost( $name, &$value ) {
	
		if( !is_array($value) )
			return (string)$value;
		
		foreach( $value as &$v)
			$v = (string)$v;
		
		return $value;
	}
	
	public function render() {
		$this->values = self::parseArrayInput( $this->values, array($this->selected=>true), $this->property, $this->keyProperty );
		//self::setSelected( $this->values, $this->selected, $this->keyProperty );
		$renderer = $this->getRenderer();
		$renderer->assign('input', $this );
		return $renderer->fetch('Input/Select.html');
	}
	

}
?>